using System;
using System.Collections.Generic;
using Com.ContentRendering.Api.RenderEngine;
using ContentRenderingApi.Data;

namespace ContentRenderingApi.Envelope
{
    /// <summary/>
    internal class InputDataEnvelope : InputDataInterface
    {
        private AbstractData _data;

        /// <summary/>
        internal InputDataEnvelope(AbstractData data)
        {
            this._data = data;
        }

        /// <summary/>
        IEnumerator<InputDataInterface> InputDataInterface.FetchParentData(int level)
        {
            return InputDataEnvelope.ConvertEnumerator(this._data.FetchParentData(level));
        }

        /// <summary/>
        IEnumerator<InputDataInterface> InputDataInterface.FetchChildData(string name)
        {
            return InputDataEnvelope.ConvertEnumerator(this._data.FetchChildData(name));
        }

        /// <summary/>
        IEnumerator<InputDataInterface> InputDataInterface.FetchRootData(string name)
        {
            return InputDataEnvelope.ConvertEnumerator(this._data.FetchRootData(name));
        }

        /// <summary/>
        private static IEnumerator<InputDataInterface> ConvertEnumerator(IEnumerator<AbstractData> enumerator)
        {
            List<InputDataInterface> inputDatas = new List<InputDataInterface>();

            // move each element to the nodes container
            while (enumerator.MoveNext())
            {
                inputDatas.Add(new InputDataEnvelope(enumerator.Current));
            }

            return inputDatas.GetEnumerator();
        }

        /// <summary/>
        string InputDataInterface.Value
        {
            get { return this._data.Value; }
        }
    }
}
